package com.hzjh.base.web.process

import android.util.ArrayMap
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ProcessUtils
import com.hzjh.base.web.util.WebLog

class BridgeUtil {
    companion object {
         private val TAG = "GuangWebView"
         fun autoRegist(): ArrayMap<String, IBridgeCommand> {
             // web jsbridge command auto load
             val commandMap = ArrayMap<String, IBridgeCommand>()
             commandMap[BridgeCommand.FlowBusBridge] = FlowBusBridge()
             commandMap[BridgeCommand.CommonBridge] = CommonBridge()
             return commandMap
        }

        fun isMainProcess():Boolean{
            val ifMainProcess= ProcessUtils.getCurrentProcessName()==AppUtils.getAppPackageName()
            WebLog.d(TAG, "isMainProcess() 当前进程：${ProcessUtils.getCurrentProcessName()}")
            return ifMainProcess
        }

        /**
         *  flowBusKey 通知的key
         *  data 推送内容
         */
        fun sendFlowBus(flowBusKey: String?,data: Any?=null){
            if(isMainProcess()) {
                //主进程向web进程发送通知
                BridgeCommandHandler.getInstance().sendCommand(
                    BridgeMessage(
                        name = BridgeCommand.FlowBusBridge,
                        data = mapOf("flowBusEvent" to flowBusKey, "params" to data)
                    )
                )
            }else{
                //web进程向主进程发送通知
                BridgeInvokeDispatcher.getInstance().sendCommand(
                    BridgeMessage(
                        name = BridgeCommand.FlowBusBridge,
                        data= mapOf("flowBusEvent" to flowBusKey, "params" to data)))
            }
        }

        /**
         *  commonKey 通用key
         *  data 内容
         */
        fun sendCommonData(commonKey: String?,data: Any?=null,ifOnlyWebProcess:Boolean=false){
            if(isMainProcess()) {
                //主进程向web进程发送通知
                BridgeCommandHandler.getInstance().sendCommand(
                    BridgeMessage(
                        name = BridgeCommand.CommonBridge,
                        data = mapOf("commonKey" to commonKey, "params" to data)
                    )
                )
            }else{
                if(ifOnlyWebProcess){
                    //只向web发送 不向主进程发送
                    return
                }
                //web进程向主进程发送通知
                BridgeInvokeDispatcher.getInstance().sendCommand(
                    BridgeMessage(
                        name = BridgeCommand.CommonBridge,
                        data= mapOf("commonKey" to commonKey, "params" to data)))
            }
        }

        /**
         * 根据key跨进程获取数据
         */
        fun getValueForKey(key: String?):String{
            return if(isMainProcess()) {
                //主进程向web进程发送通知
                BridgeCommandHandler.getInstance().sendValueForKey(key)
            }else{
                //web进程向主进程发送通知
                BridgeInvokeDispatcher.getInstance().sendValueForKey(key)
            }
        }
    }
}